#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer();
    timer->setInterval(2000);
    timer->start();
    ui->vSliderVolume->setVisible(false);
    QString fileName = "D:/Music";
    QDir dir(fileName);
    QStringList nameFilters;
    nameFilters << "*.mp3";
    QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
    ui->lwMusicList->addItems(files);
    m_playPath = fileName;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btnPlay_clicked()//播放暂停
{
    if(QMediaPlayer:: PlayingState == m_mediaPlayer.state()){
        m_mediaPlayer.pause();
        ui->btnPlay->setText("Play");
    }
    else{
        m_mediaPlayer.play();
        ui->btnPlay->setText("Pause");
    }
}


void MainWindow::on_hSliderPlayProgress_sliderMoved(int position)//通过slider值改变音量大小
{
    m_mediaPlayer.setPosition(m_mediaPlayer.duration()*position/100);
}
void MainWindow::setSliderValue()
{
    ui->hSliderPlayProgress->setValue(m_mediaPlayer.position()*100/m_mediaPlayer.duration());
    if(m_mediaPlayer.position()>=m_mediaPlayer.duration()){
            on_btnNextMusic_clicked();
      }
}

void MainWindow::on_vSliderVolume_sliderMoved(int position)
{
    m_mediaPlayer.setVolume(position);
}

void MainWindow::on_lwMusicList_itemDoubleClicked(QListWidgetItem *item)//双击播放列表播放
{
    m_PlayRow = ui->lwMusicList->row(item);
    m_mediaPlayer.stop();
    m_mediaPlayer.setMedia(QUrl::fromLocalFile(m_playPath+"/"+item->text()));
    m_mediaPlayer.play();
    ui->hSliderPlayProgress->setValue(m_mediaPlayer.position());
    ui->btnPlay->setText("Pause");
    connect(timer, SIGNAL(timeout()), this, SLOT(setSliderValue()));
    connect(&m_mediaPlayer,SIGNAL(durationChanged(qint64)),this,SLOT(getduration()));
    connect(&m_mediaPlayer,SIGNAL(positionChanged(qint64)),this,SLOT(setPlayTime()));
}

void MainWindow::on_btnAddMusic_clicked()//向列表中添加音乐
{
    QString fileName = QFileDialog::getExistingDirectory(NULL,"Select Music Dir",".",NULL);
    QDir dir(fileName);
    QStringList nameFilters;
    nameFilters << "*.mp3";
    QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
    ui->lwMusicList->addItems(files);
    m_playPath = fileName;
}

void MainWindow::on_btnPreMusic_clicked()//上一曲
{
    if(m_PlayRow == 0){
        m_PlayRow = ui->lwMusicList->count() - 1;
    }
    else{
        m_PlayRow--;
    }
    QListWidgetItem *item = ui->lwMusicList->item(m_PlayRow);
    item->setSelected(true);
    m_mediaPlayer.stop();
    m_mediaPlayer.setMedia(QUrl::fromLocalFile(m_playPath+"/"+item->text()));
    m_mediaPlayer.play();
}

void MainWindow::on_btnNextMusic_clicked()//下一曲
{
    if(m_PlayRow + 1 == ui->lwMusicList->count()){
        m_PlayRow = 0;
    }
    else{
        m_PlayRow++;
    }
    QListWidgetItem *item = ui->lwMusicList->item(m_PlayRow);
    item->setSelected(true);
    m_mediaPlayer.stop();
    m_mediaPlayer.setMedia(QUrl::fromLocalFile(m_playPath+"/"+item->text()));
    m_mediaPlayer.play();
}

void MainWindow::on_btnVolume_clicked()//音量条弹出和隐藏
{
    if(ui->vSliderVolume->isVisible()){
        ui->vSliderVolume->setVisible(false);
    }else{
        ui->vSliderVolume->setVisible(true);
    }
}

void MainWindow::on_vSliderVolume_sliderReleased()//自动隐藏音量条
{
     ui->vSliderVolume->setVisible(false);
}
QString MainWindow::settime(int time)
{
    int h,m,s;
    time /= 1000;  //获得的时间是以毫秒为单位的
    h = time/3600;
    m = (time-h*3600)/60;
    s = time-h*3600-m*60;
    return QString("%1:%2:%3").arg(h).arg(m).arg(s);//获得当前播放时间
}

void MainWindow::getduration()
{
    playtime = m_mediaPlayer.duration();
    ui->lblMusicTime->setText(settime(playtime));//歌曲长度
}
void MainWindow::setPlayTime()
{
    ui->lblPlayTime->setText(settime(m_mediaPlayer.position()));//当前播放进度
}

